package com.smcdd.cdb.dbo.cdb_dbo.ui.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 作者：Administrator on 2017/7/25 17:13
 * 邮箱：zhanghuaiha@gmail.com
 */

public class DateUtils {

    public static String getTime(long time) {//可根据需要自行截取数据显示
        Date date = new Date(time);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    public static String getTime(long time,String type) {//可根据需要自行截取数据显示
        Date date = new Date(time);
        SimpleDateFormat format = new SimpleDateFormat(type);
        return format.format(date);
    }

    /**
     * 获取当前时间的前一天
     * @param time
     * @param type
     * @return
     */
    public static String getNextDay(long time,String type,int day) {
        Date date = new Date(time);
        SimpleDateFormat format = new SimpleDateFormat(type);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -day);
        date = calendar.getTime();
        return format.format(date);
    }

    /**
     * 获取当前时间的下一天
     * @param time
     * @param type
     * @return
     */
    public static String getTotDay(long time,String type,int day) {
        Date date = new Date(time);
        SimpleDateFormat format = new SimpleDateFormat(type);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, +day);
        date = calendar.getTime();
        return format.format(date);
    }


    public static String getMM(long time) {//可根据需要自行截取数据显示
        Date date = new Date(time);
        SimpleDateFormat format = new SimpleDateFormat("HH");
        return format.format(date);
    }

    public static long getTime_long(String time){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return  format.parse(time).getTime();

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * result大于0，则t1>t2；

     result等于0，则t1=t2；

     result小于0，则t1<t2；
     */
    public static long S1compareS2(String s1,String s2,String type){
        SimpleDateFormat sdf = new SimpleDateFormat(type);
        Date d1 = null;
        Date d2 = null;
        try {
            d1 = sdf.parse(s1);
            d2 = sdf.parse(s2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long result = d1.getTime()-d2.getTime();
        return result;
    }
}
